package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.config.IotModbusServerConfig;
import com.ruoyi.system.domain.IotVariable;
import com.ruoyi.system.domain.vo.IotDeviceVo;
import com.ruoyi.system.domain.vo.IotVariableVo;
import com.ruoyi.system.domain.vo.ValveParams;
import com.ruoyi.system.mapper.IotDeviceMapper;
import com.ruoyi.system.mapper.IotVariableMapper;
import com.ruoyi.system.service.IIotModbusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IotModbusServiceImpl implements IIotModbusService {

    @Autowired
    private IotDeviceMapper iotDeviceMapper;

    @Autowired
    private IotVariableMapper iotVariableMapper;

    @Autowired
    private IotModbusServerConfig iotModbusServerConfig;


    @Override
    public void editValve(ValveParams valveParams) {
        IotDeviceVo iotDeviceVo = iotDeviceMapper.selectIotDeviceByDeviceId(valveParams.getDeviceId());
        if (iotDeviceVo == null) {
            throw new ServiceException("设备数据不存在");
        }
//        IotVariable iotVariable = iotVariableMapper.selectIotVariableById(valveParams.getVariableId());
        IotVariableVo iotVariableVo = iotVariableMapper.selectIotVariableVoById(valveParams.getVariableId());
        if (iotVariableVo == null) {
            throw new ServiceException("变量数据不存在");
        }
        // 01 06 00 1B 数据 数据 数据 数据 digit
        String position = String.format("%04x", iotVariableVo.getSerialNum() - 1);
        String data = "";
        // 16位 无符号
        if (iotVariableVo.getDataFormatValue() == 1) {
            data = shortToHex(Short.parseShort(valveParams.getVariableValue()));
        } else if (iotVariableVo.getDataFormatValue() == 2) {
            // 16位 有符号
            data = shortToHex(Short.parseShort(valveParams.getVariableValue()));
        } else if (iotVariableVo.getDataFormatValue() == 3) {
            // 16位 BC         D
            data = shortToHex(Short.parseShort(valveParams.getVariableValue()));
        } else if (iotVariableVo.getDataFormatValue() == 4) {
            // 32位浮点数
            data = floatToHex(Float.parseFloat(valveParams.getVariableValue()));
            if (Float.parseFloat(valveParams.getVariableValue()) <= 0) {
                data = "00000000";
            }

        } else if (iotVariableVo.getDataFormatValue() == 5) {
            // 32位整数

        }
        if(StringUtils.isEmpty(data)){
            return;
        }

        iotModbusServerConfig.getMiiServer().sender().editValve(iotVariableVo.getDataFormatValue(), iotDeviceVo.getSn(), iotDeviceVo.getSlaveAddress(), position, data);
        iotModbusServerConfig.getMiiServer().sender().testIot(valveParams.getSn(), valveParams.getSlaveAddress());
    }

    private String floatToHex(float value) {
        float floatValue = value;
        int intBits = Float.floatToIntBits(floatValue);
        String hexString = Integer.toHexString(intBits);
        return hexString;
    }

    private String shortToHex(short value) {
        return String.format("%04X", value );
    }

    public static void main(String[] args) {
        short shortValue = 17; // 16位整数的最大值
        String hexString = String.format("%04X", shortValue );
        System.out.println("Hexadecimal representation: " + hexString);
    }

}
